package be.teletask.onvif.parsers;

import be.teletask.onvif.models.GetAudioDecoderConfigurations;
import be.teletask.onvif.models.GetAudioOutputConfiguration;
import be.teletask.onvif.responses.OnvifResponse;
import be.teletask.onvif.util.XmlToJsonUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tomas Verhelst on 04/09/2018.
 * Copyright (c) 2018 TELETASK BVBA. All rights reserved.
 */
public class OnvifMediaAudioDecoderConfigurationsParser extends OnvifParser<List<GetAudioDecoderConfigurations>> {

    //Constants
    public static final String TAG = OnvifMediaAudioDecoderConfigurationsParser.class.getSimpleName();

    @Override
    public List<GetAudioDecoderConfigurations> parse(OnvifResponse response) {
        List<GetAudioDecoderConfigurations> audioDecoderConfigurations = new ArrayList<>();
        JSONObject bodyObjet = XmlToJsonUtils.xmlToJson(response.getXml());

        JSONArray configurationsArray = bodyObjet.getJSONObject("GetAudioDecoderConfigurationsResponse").getJSONArray("Configurations");

        for (int i = 0; i < configurationsArray.size(); i++) {
            // 获取每个JSONObject
            JSONObject configurationObject = configurationsArray.getJSONObject(i);

            String token = configurationObject.getString("@token");
            String name = configurationObject.getString("Name");
            Integer useCount = 0;
            if(configurationObject.containsKey("UseCount")) {
                useCount = configurationObject.getInteger("UseCount");
            }
            GetAudioDecoderConfigurations audioDecoder = new GetAudioDecoderConfigurations();
            audioDecoder.setToken(token);
            audioDecoder.setName(name);
            audioDecoder.setUseCount(useCount);

            audioDecoderConfigurations.add(audioDecoder);
        }

        return audioDecoderConfigurations;
    }

}
